﻿using System;
using Ext.Net;

namespace ERP.Modules
{
    public partial class MCK_SchClass : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                GetData();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }

        #region Event
        protected void StoreMCK_SchClass_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetData();
        }

        protected void MCK_SchClassDblClick(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            int SchClass_ID = int.Parse(e.ExtraParams["SchClass_ID"]);
            DataDetail(SchClass_ID);
            WindowsMCK_SchClass.Show();
        }

        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            WindowsMCK_SchClass.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grMCK_SchClass.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    ClearTextBox();
                    DataDetail(int.Parse(row.RecordID));
                    WindowsMCK_SchClass.Show();
                    sm.UpdateSelection();
                }
        }

        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grMCK_SchClass.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    try
                    {
                        BLL.MCK_SchClass_BLL.Delete_MCK_SchClass(int.Parse(row.RecordID));
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetData();
        }

        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtSchClass_ID.Text == string.Empty)
            {
                Insert_SchClass();
            }
            else
            {
                Update_SchClass();
            }
        }

        #endregion

        #region

        void GetData()
        {
            var dt = BLL.MCK_SchClass_BLL.GetData_MCK_SchClass(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                StoreMCK_SchClass.DataSource = dt;
                StoreMCK_SchClass.DataBind();
            }
        }

        private void DataDetail(int id)
        {
            var dt = BLL.MCK_SchClass_BLL.GetDataRow_MCK_SchClassDetail(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                txtSchClass_ID.Text = dt.Rows[0]["SchClass_ID"].ToString();
                txtSchClass_Name.Text = dt.Rows[0]["SchClass_Name"].ToString();
                txtStartTime.Text = dt.Rows[0]["StartTime"].ToString();
                txtEndTime.Text = dt.Rows[0]["EndTime"].ToString();
                txtLateMinutes.Text = dt.Rows[0]["LateMinutes"].ToString();
                txtEarlyMinutes.Text = dt.Rows[0]["EarlyMinutes"].ToString();
                ckCheckIn.Checked = dt.Rows[0]["CheckIn"].ToString() == "1";
                ckCheckOut.Checked = dt.Rows[0]["CheckOut"].ToString() == "1";
                txtCheckInTime1.Text = dt.Rows[0]["CheckInTime1"].ToString();
                txtCheckInTime2.Text = dt.Rows[0]["CheckInTime2"].ToString();
                txtCheckOutTime1.Text = dt.Rows[0]["CheckOutTime1"].ToString();
                txtCheckOutTime2.Text = dt.Rows[0]["CheckOutTime2"].ToString();
                txtWorkDay.Text = dt.Rows[0]["WorkDay"].ToString();
                txtWorkMins.Text = dt.Rows[0]["WorkMins"].ToString();
                var week = dt.Rows[0]["Weekend"].ToString();
                ckDay1.Checked = week.IndexOf("1") != -1;
                ckDay2.Checked = week.IndexOf("2") != -1;
                ckDay3.Checked = week.IndexOf("3") != -1;
                ckDay4.Checked = week.IndexOf("4") != -1;
                ckDay5.Checked = week.IndexOf("5") != -1;
                ckDay6.Checked = week.IndexOf("6") != -1;
                ckDay7.Checked = week.IndexOf("7") != -1;
            }
        }

        private void Insert_SchClass()
        {
            try
            {
                var obj = new ET.MCK_SchClass_ET
                {
                    SchClass_Name = txtSchClass_Name.Text,
                    StartTime = txtStartTime.Text,
                    EndTime = txtEndTime.Text,
                    LateMinutes = int.Parse(txtLateMinutes.Text),
                    EarlyMinutes = int.Parse(txtEarlyMinutes.Text),
                    CheckIn = ckCheckIn.Checked ? 1 : 0,
                    CheckOut = ckCheckOut.Checked ? 1 : 0,
                    CheckInTime1 = txtCheckInTime1.Text,
                    CheckInTime2 = txtCheckInTime2.Text,
                    CheckOutTime1 = txtCheckOutTime1.Text,
                    CheckOutTime2 = txtCheckOutTime2.Text,
                    WorkDay = int.Parse(txtWorkDay.Text),
                    WorkMins = int.Parse(txtWorkMins.Text),
                    Weekend = ((ckDay1.Checked ? "1," : "") + (ckDay2.Checked ? "2," : "") + (ckDay3.Checked ? "3," : "") + (ckDay4.Checked ? "4," : "") + (ckDay5.Checked ? "5," : "") + (ckDay6.Checked ? "6," : "") + (ckDay7.Checked ? "7" : "")).Remove(((ckDay1.Checked ? "1," : "") + (ckDay2.Checked ? "2," : "") + (ckDay3.Checked ? "3," : "") + (ckDay4.Checked ? "4," : "") + (ckDay5.Checked ? "5," : "") + (ckDay6.Checked ? "6," : "") + (ckDay7.Checked ? "7" : "")).Length - 1)
                };
                BLL.MCK_SchClass_BLL.Insert_MCK_SchClass(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                GetData();
                ClearTextBox();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }

        private void Update_SchClass()
        {
            try
            {
                var obj = new ET.MCK_SchClass_ET
                {
                    SchClass_ID = int.Parse(txtSchClass_ID.Text),
                    SchClass_Name = txtSchClass_Name.Text,
                    StartTime = txtStartTime.Text,
                    EndTime = txtEndTime.Text,
                    LateMinutes = int.Parse(txtLateMinutes.Text),
                    EarlyMinutes = int.Parse(txtEarlyMinutes.Text),
                    CheckIn = ckCheckIn.Checked ? 1 : 0,
                    CheckOut = ckCheckOut.Checked ? 1 : 0,
                    CheckInTime1 = txtCheckInTime1.Text,
                    CheckInTime2 = txtCheckInTime2.Text,
                    CheckOutTime1 = txtCheckOutTime1.Text,
                    CheckOutTime2 = txtCheckOutTime2.Text,
                    WorkDay = int.Parse(txtWorkDay.Text),
                    WorkMins = int.Parse(txtWorkMins.Text),
                    Weekend = ((ckDay1.Checked ? "1," : "") + (ckDay2.Checked ? "2," : "") + (ckDay3.Checked ? "3," : "") + (ckDay4.Checked ? "4," : "") + (ckDay5.Checked ? "5," : "") + (ckDay6.Checked ? "6," : "") + (ckDay7.Checked ? "7" : "")).Remove(((ckDay1.Checked ? "1," : "") + (ckDay2.Checked ? "2," : "") + (ckDay3.Checked ? "3," : "") + (ckDay4.Checked ? "4," : "") + (ckDay5.Checked ? "5," : "") + (ckDay6.Checked ? "6," : "") + (ckDay7.Checked ? "7" : "")).Length - 1)
                };
                BLL.MCK_SchClass_BLL.Update_MCK_SchClass(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Sửa thành công !");
                GetData();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }

        private void ClearTextBox()
        {
            FormPanelMCK_SchClass.Reset();

            txtSchClass_ID.Text = string.Empty;
            ckCheckIn.Checked = false;
            ckCheckOut.Checked = false;
        }
        #endregion
        #region[Search]
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var dt = BLL.MCK_SchClass_BLL.GetData_MCK_SchClass(txtSearch.Text);
            if (dt != null && dt.Rows.Count > 0)
            {
                StoreMCK_SchClass.DataSource = dt;
                StoreMCK_SchClass.DataBind();
            }
        }
        #endregion
    }
}